package cn.gjculture.shopping.api.service.impl;

import cn.gjculture.shopping.api.dao.CustomerDao;
import cn.gjculture.shopping.api.dao.PicTxtMessageDao;
import cn.gjculture.shopping.api.dao.PicTxtMessageMapper;
import cn.gjculture.shopping.api.entity.Customer;
import cn.gjculture.shopping.api.entity.Return;
import cn.gjculture.shopping.api.entity.wx.PicTxtMessage;
import cn.gjculture.shopping.api.service.ServiceMessageService;
import cn.gjculture.shopping.api.service.WXService;
import cn.gjculture.shopping.api.util.DateUtil;
import cn.gjculture.shopping.api.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ServiceMessageServiceImpl implements ServiceMessageService {

    @Autowired
    PicTxtMessageDao picTxtMessageDao;

    @Autowired
    PicTxtMessageMapper picTxtMessageMapper;

    @Autowired
    CustomerDao customerDao;

    @Autowired
    WXService wxService;

    @Override
    public int insert(PicTxtMessage picTxtMessage) {
        picTxtMessage.setId(UUIDUtil.getUUID());
        picTxtMessage.setInsertTime(DateUtil.getTimestamp());
        picTxtMessage.setUpdateTime(DateUtil.getTimestamp());
        picTxtMessage.setBelong(PicTxtMessage.MessageType.SERVICE.name());
        picTxtMessageDao.insert(picTxtMessage);
        return 1;
    }

    @Override
    public int update(PicTxtMessage picTxtMessage) {
        return 0;
    }

    @Override
    public List<PicTxtMessage> query(PicTxtMessage picTxtMessage) {
        List<PicTxtMessage> picTxtMessages=picTxtMessageMapper.query(picTxtMessage);
        return picTxtMessages;
    }

    @Override
    public PicTxtMessage queryById(String id) {
        return picTxtMessageDao.queryById(id);
    }

    @Override
    public int delete(String id, Return ret) {
        return picTxtMessageDao.delete(id);
    }

    @Override
    public int send(String id) {
        PicTxtMessage message=picTxtMessageDao.queryById(id);
        List<Customer> customers=customerDao.queryAllOpenId();
        wxService.sendServiceMessage(message,customers);
        return 1;
    }
}
